<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<div id="boxdiv" style="width:510px;height:450px;">
<div style="padding:10px;">
键:<input type="text" id="k1" /> = 值:<input type="text" id="v1"/>
</div>
<div style="padding:10px;">
键:<input type="text" id="k2" /> = 值:<input type="text" id="v2"/>
</div>
<div style="padding:10px;">
键:<input type="text" id="k3" /> = 值:<input type="text" id="v3"/>
</div>
</div>
<input type="button" onclick="addbox('','');" value="继续添加"/>
<input type="button" onclick="clean();" value="清空" />
<input type="button" onclick="checkjson();" value="检查结果"/>
<script>
var count = 3;
var str = '${t}';
if(null!=str&&''!=str){
	try{
		str =  $.parseJSON(str);
		var n = 1;
		for(var key in str){ 
	        $("#k"+n).val(key);
	        $("#v"+n).val(str[key]);
	        if(n++>count){
	        	addbox(key,str[key]);
	        }
	    } 
	}catch(e){
		str= {}
	}
}

	function addbox(key,val){
		count++;
		$("#boxdiv").append("<div style='padding:10px;'>键:<input type='text' id='k"+count+"' value='"+key+"' /> = 值:<input type='text' id='v"+count+"' value='"+val+"'/></div>");
	}
	function checkjson(){
		var json = maptojson();
		alert(json);
	}
	
	function maptojson(){
		var json = "";
		for(var i = 1 ; i <= count ; i++ ){
			var key = $("#k"+i).val();
			var val = $("#v"+i).val();
			if(null!=key&&''!=key){
				json = json + ",\""+key+"\":\""+val+"\"";
			}
		}
		json = "{"+json.substring(1,json.length)+"}";
		return json;
	}
	
	function clean(){
		for(var i = 1 ; i <= count ; i++ ){
			$("#k"+i).val("");
			$("#v"+i).val("");
		}
	}

</script>